﻿$(document).ready(function () {
    //    page.commonUserControl = new NDUAP.Selector({ divID: "userId", typeID: 1, totalSelectNum: 1, quickBoxPositionX: -285, width: 150, searchData: page.userList });
    $("#ul-nav li:not(.nav-header)").eq(0).addClass("active");
    page.init();
});

var page = {
    pageIndex: 0,
    pageSize: 10,
    url: "/handler/admin/user.ashx?action=",
    popU: null,

    init: function () {
        page.getUserList();
    },

    getUserList: function (pageIndex) {
        var user = $.trim($("#userName").val());
        var deptName = $.trim($("#deptName").val());
        var jobName = $.trim($("#jobName").val());
        var status = $("#status").val();
        var gender = $("#gender").val();
        var usertype = $("#usertype").val();

        if (pageIndex != undefined)
            page.pageIndex = pageIndex;

        $.get(page.url + "getUserList", { user: user, status: status, gender: gender, deptName: deptName,
            jobName: jobName, usertype: usertype,
            pageIndex: page.pageIndex, pageSize: page.pageSize, r: Math.random()
        }, function (data) {
            $("#result").html(TrimPath.processDOMTemplate("userList", data));
            page.pager(data.count);
            page.list = data.list;
        }, "json");
    },

    pager: function (count) {
        if (count == 0) {
            $("#pagination").empty();
            return;
        }
        $("#pagination").pagination(count, {
            num_edge_entries: 2,
            num_display_entries: 10,
            current_page: page.pageIndex,
            items_per_page: page.pageSize,
            callback: page.getUserList
        });
    },

    resetPassword: function (username, jobnumber, userId) {
        var html = [];
        html.push('<form class="form-horizontal">');
        html.push('<div class="control-group"><label class="control-label">员工</label>');
        html.push('<div class="controls"><label class="control-label" style="width:240px; text-align:left;">' + username + '<small>(' + jobnumber + ')</small></label></div>');
        html.push('</div>');
        html.push('<div class="control-group"><label class="control-label" for="newpassword">密码</label>');
        html.push('<div class="controls"><input id="newpassword" placeholder="密码" type="text" required/></div>');
        html.push('</div>');
        html.push('<div class="control-group"><label class="control-label" for="checkpassword">确认密码</label>');
        html.push('<div class="controls"><input id="checkpassword" placeholder="确认密码" type="text" required/></div>');
        html.push('</div>');
        html.push('<div class="control-group">');
        html.push('<div class="controls"><button class="btn" type="button" onclick="page.reset(' + userId + ')">确认重置</button></div>');
        html.push('</div>');
        html.push('</form>');

        page.popU = page.popU || new PopU();
        page.popU.initPop({
            type: 'string',
            title: "重置密码",
            contents: html.join(""),
            w: 400,
            drag: true
        });
        page.popU.showPop();
    },

    reset: function (userId) {
        var newpassword = $("#newpassword").val();
        var checkpassword = $("#checkpassword").val();

        if (newpassword == "") {
            alert("请输入新密码");
            $("#newpassword").focus();
            return;
        }

        if (checkpassword == "") {
            alert("请输入确认密码");
            $("#resetpassword").focus();
            return;
        }

        if (newpassword != checkpassword) {
            alert("两次密码输入不一致");
            return;
        }

        $.post(page.url + "reset", { userId: userId, newPassword: newpassword }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            alert("重置成功");
            page.popU.close();
        }, "json");
    },

    edit: function (userId) {
        var user = null;
        for (var i = 0; i < page.list.length; i++) {
            if (page.list[i].UserId == userId) {
                user = page.list[i];
                break;
            }
        }

        if (user == null) {
            alert("数据异常，请刷新页面后再试");
            return false;
        }

        page.popU = page.popU || new PopU();
        page.popU.initPop({
            type: 'string',
            title: "编辑用户",
            contents: TrimPath.processDOMTemplate("detail", user),
            w: 750,
            drag: true
        });
        page.popU.showPop();

        $("#uphone").keyup(function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).bind("paste", function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).css("ime-mode", "disabled");

        $("#ugender").val(user.Gender);

        $("#btnEdit").unbind("click").click(function () {
            page.editUser(user.UserId);
        });
    },

    editUser: function (userId) {
        var gender = $("#ugender").val();
        if (gender == 0) {
            alert("请选择性别");
            $("#ugender").focus();
            return false;
        }

        var deptName = $.trim($("#udeptName").val());
        var jobName = $.trim($("#ujobName").val());
        var phone = $.trim($("#uphone").val());
        var email = $.trim($("#uemail").val());

        $.post(page.url + "edit", { userId: userId, gender: gender, deptName: deptName, jobName: jobName, phone: phone, email: email }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            page.getUserList();
            page.popU.close();
        }, "json");
    },

    setStatus: function (status, userId) {
        $.post(page.url + "setStatus", { userId: userId, status: status }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            page.getUserList();
        }, "json");
    },

    setUserType: function (userType, userId) {
        $.post(page.url + "setUserType", { userId: userId, userType: userType }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            page.getUserList();
        }, "json");
    }
};

